package com.lunan.gulimail.product.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lunan.common.utils.PageUtils;
import com.lunan.common.utils.Query;

import com.lunan.gulimail.product.dao.CategoryDao;
import com.lunan.gulimail.product.entity.CategoryEntity;
import com.lunan.gulimail.product.service.CategoryService;

@Slf4j
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询所有商品分类，并以树状结构返回
     *
     * @return
     */
    @Override
    public List<CategoryEntity> listWithTree() {
        // 1. 查表pms_category，返回所有分类信息
        List<CategoryEntity> categoryList = this.list();
        log.info("商品所有分类：{}", categoryList);
        // 2. 过滤一级分类,parent_cid = 0；并递归获取子分类
        List<CategoryEntity> level1Category = categoryList.stream().filter(category ->
                        category.getParentCid() == 0
                ).map(category -> {
                    category.setChildrenList(getChildrenList(category, categoryList));
                    return category;
                }).sorted((category1, category2) -> {
                    // 避免最后一层空对象
                    return (category1.getSort() == null ? 0 : category1.getSort()) - (category2.getSort() == null ? 0 : category2.getSort());
                })
                .collect(Collectors.toList());
        return level1Category;
    }

    @Override
    public void removeCategoryByIds(List<Long> categoryIds) {
        // 使用逻辑删除
        this.removeByIds(categoryIds);
    }

    /**
     * 递归获取子分类
     *
     * @param rootCategory
     * @param allCategory
     * @return
     */
    private List<CategoryEntity> getChildrenList(CategoryEntity rootCategory, List<CategoryEntity> allCategory) {
        // 1. 过滤分类，若遍历的父分类Id为当前根节点的分类id
        List<CategoryEntity> childrenList = allCategory.stream().filter(category ->
                        category.getParentCid() == rootCategory.getCatId()
                ).map(category -> {
                    category.setChildrenList(getChildrenList(category, allCategory));
                    return category;
                }).sorted((category1, category2) -> {
                    // 避免最后一层空对象
                    return (category1.getSort() == null ? 0 : category1.getSort()) - (category2.getSort() == null ? 0 : category2.getSort());
                })
                .collect(Collectors.toList());
        return childrenList;
    }
}